package api

import (
	"gitcode.com/G-YT/iHealth25/server/global"
	"gitcode.com/G-YT/iHealth25/server/model/common/response"
	"gitcode.com/G-YT/iHealth25/server/plugin/weixin/model/weiXin/request"
	"gitcode.com/G-YT/iHealth25/server/plugin/weixin/service"
	"github.com/gin-gonic/gin"
)

type WeChatApi struct{}

// GetOpenID 获取微信OpenID
// @Tags WeiXin
// @Summary 获取微信OpenID
// @Description 通过微信code换取用户OpenID
// @Accept json
// @Produce json
// @Param data body request.WechatCode true "微信code"
// @Success 200 {object} response.Response{data=string} "{"success":true,"data":"OPENID","msg":"获取成功"}"
// @Router /weiXin/getOpenID [post]
func (w *WeChatApi) GetOpenID(c *gin.Context) {
	var req request.WechatCode
	// 绑定请求参数（参考引用1/4的body绑定方式）
	if err := c.ShouldBindJSON(&req); err != nil {
		response.FailWithMessage("参数错误:"+err.Error(), c) // 错误响应格式见引用1/6
		return
	}
	// 通过微信服务获取OpenID（需实现service层逻辑）
	openID, err := service.WechatService.GetOpenIDByCode(req.Code)
	if err != nil {
		global.GVA_LOG.Error("微信接口调用失败: " + err.Error()) // 日志记录见引用6
		response.FailWithMessage("微信授权失败", c)
		return
	}
	// 返回成功响应（格式符合引用3/6规范）
	response.OkWithDetailed(openID, "获取成功", c) // 响应结构见引用2/3
}
